<script>
	import util from '@/common/util'
	import Vue from 'vue'
	import {
		getMerchant,
		getPlatDetail,
		getOpenId
	} from '@/api/common.js'
	import {
		loginOpenId
	} from '@/api/user/login'
	import {
		getPetUserInfo
	} from '@/api/pet/my/index'
	// import {
	// 	listFooter
	// } from '@/api/decorate'
	import env from '@/config/env.js'
	import {
		getUserHome
	} from '@/api/my/home.js'
	// const config = require('@/packagesB/utils/config.js');

	export default {
		onLaunch: function() {
			this.initChat()
			this.$store.dispatch('getAddress')
			this.globalData.deviceInfo = uni.getDeviceInfo()
			 console.log('设备信息',this.globalData.deviceInfo)
			// #ifdef H5
			if (util.isWeiXin()) {
				util.getUserinfo()
			}
			// #endif

			let that = this;
			Vue.prototype.$getUserHome = this.getUserHome
			this.getPlatDetail()
			// this.listFooter()
			// that.$store.dispatch('getLocation')
			
			// #ifdef MP-WEIXIN
			that.$store.dispatch('getOpenid')
			this.login()
			// #endif
			
			uni.getSystemInfo({
				success: function(e) {
					that.globalData.platform = e.platform
					that.globalData.deviceId = e.deviceId
					// #ifdef APP-PLUS
					// #endif
					that.globalData.system = e.system.substr(0, e.system.lastIndexOf("."))
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					Vue.prototype.platform = wx.getDeviceInfo().platform;
					uni.getUpdateManager().onCheckForUpdate(function(res) {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							//如果有新版本
							// 小程序有新版本，会主动触发下载操作（无需开发者触发）
							uni.getUpdateManager().onUpdateReady(function() {
								//当新版本下载完成，会进行回调
								uni.showModal({
									title: '更新提示',
									content: '新版本已经准备好，单击确定重启应用',
									showCancel: false,
									success: function(res) {
										if (res.confirm) {
											uni.getUpdateManager()
												.applyUpdate();
										}
									}
								});
							});

							// 小程序有新版本，会主动触发下载操作（无需开发者触发）
							uni.getUpdateManager().onUpdateFailed(function() {
								//当新版本下载失败，会进行回调
								uni.showModal({
									title: '提示',
									content: '请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开',
									showCancel: false
								});
							});
						}
					});

					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				},
			})
		},
		onShow: function() {},
		onHide: function() {},
		globalData: {
			openid: '',
			deviceId: '',
			platform: '',
			system: '',
			isClick: false,
			version: 'release',
			corpId: "",
			svrUrl: "",
			name: "",
			description: "",
			logo: require("@/static/logo.png"),
			tagStyle: {
				table: 'box-sizing: border-box; border-top: 1px solid #dfe2e5; border-left: 1px solid #dfe2e5;',
				th: 'border-right: 1px solid #dfe2e5; border-bottom: 1px solid #dfe2e5;',
				td: 'border-right: 1px solid #dfe2e5; border-bottom: 1px solid #dfe2e5;',
				li: 'margin: 5px 0;'
			},
			Service:{},
			deviceInfo:{}, //设备信息
		},
		methods: {
			initChat() {
				// config.setLogStdout = (level, content) => {
				// 	logArr.unshift(content);
				// 	logArr.splice(500)
				// };
				// config.logLevel = 0;
				// console.log(config,'asdfasdf')
				// this.globalData.Service = require('@/common/services.js')(config);
			},
			// 获取平台信息
			getPlatDetail() {
				getPlatDetail({
					id: env[env.mchType].platid
				}).then(res => {
					if (res.status != 'PASS' || res.enable == 0 || res.systemEnable == 0 || !res.id) {
						uni.showModal({
							title: '提示',
							content: `平台已经被关闭了，点击确定关闭程序`,
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									uni.exitMiniProgram({
										success: (res) => {}
									})
								}
							}
						});
					} else {
						uni.setStorageSync('platInfo',res)
					}
				})
			},
			// 获取底部导航栏
			// async listFooter() {
			// 	var res = await listFooter({

			// 	})
			// 	this.$store.commit('SET_TAB_BARS', res || [])
			// },
			// 获取用户信息
			async getUserHome(cb) {
				var token = uni.getStorageSync('token')
				if (!token) {
					return
				}
				let api = env.mchType=='chongwu'?getPetUserInfo:getUserHome
				var res = await api()
				uni.setStorageSync('user', env.mchType=='chongwu'?res:res.user)
				if(res.channel && res.channel?.status == 'PASS'){
					uni.setStorageSync('shareId',res.channel.id)
				}
				if(typeof cb == 'function'){
					cb(res)
				}
				return env.mchType=='chongwu'?res:res.user
			},
			// 登录
			login(cb) {
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: async loginRes => {
						let data = await getOpenId({
							code: loginRes.code,
							mchId: env.mchId
						})
						console.log(env.mchType,'asdfas')
						// if(env.mchType == 'ceshi'){
						// 	data.openid = "on3eR6zuk2pdEv-C5BlXJ6NvuwbU"
						// }
						if (data.openid) {
							this.globalData.openid = data.openid
							let isOut = util.getLocalCache('isOut')
							if (!isOut || isOut == undefined) {
								let loginData = await loginOpenId({
									openId: data.openid
								})
								if (loginData) {
									util.setLocalCache('token', loginData.access_token)
									util.setLocalCache('mchId', loginData.companyId)
									this.getUserHome()
								}
							}
							this.$isResolve()
						} else {
							if (typeof cb == 'function') {
								cb && cb()
							}
						}
						this.$isResolve()
					}
				})
				// #endif

				// 	// #ifdef APP || H5 
				// 	this.$isResolve()
				// 	// #endif
			}

		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import './common/css/common.css';
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import './common/css/resetColor.scss';
	@import 'uni.scss';

	* {
		box-sizing: border-box;
	}

	view {
		word-break: break-all;
	}

	page {
			--primaryBg: #FE4338;
			--primaryGradBg: linear-gradient(135deg, #FF7154 0%, #FF2020 100%);
			--tagBg: #FFF0EF;
			--statusBg1: #FFAA00;
			--statusBg2: #34B31B;
			--statusBg3: #CB9865;
			// --btnColor: #ffffff;
			--btnColor: #000;
			--h1Color: #111111;
			--h2Color: #666666;
			--h3Color: #999999;
			--h4Color: #BBBBBB;
		}
		
		// page {
		// 	--primaryBg: #FF9002; //主题色
		// 	//--primaryBg: #FFE359; //主题色
		// 	--primaryGradBg: #FFE359; //按钮渐变色
		// 	--tagBg: #FFF0EF; //标签背景色
		// 	--statusBg1: #FFAA00; 
		// 	--statusBg2: #34B31B;
		// 	--statusBg3: #CB9865;
		// 	--statusBg4: #ff9d51;
		// 	--btnColor: #222222; //按钮文字颜色
		// 	--h1Color: #111111; //
		// 	--h2Color: #666666;
		// 	--h3Color: #999999;
		// 	--h4Color: #BBBBBB;
		// }
</style>